package com.yeren.czbk.collection;

import java.util.Arrays;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.yeren.czbk.collection.entity.Cuser;

public class App {
	private static SessionFactory ssessionFactory;
	// 初始化ssessionFactory
	static {
		Configuration cfg = new Configuration();
		cfg.configure();// 读取配置文件
		ssessionFactory = cfg.buildSessionFactory();// 根据配置生成session工厂
	}

//	@Test
	public void testSave() throws Exception {

		Cuser user = new Cuser();
		user.setName("李8");
		//set集合
		user.getAddressSet().add("湖北武汉黄陂区");
		user.getAddressSet().add("吉林长春经济开发区");
		user.getAddressSet().add("上海闵行");
		//List集合
		user.getAddressList().add("北京");
		user.getAddressList().add("北京");
		user.getAddressList().add("长沙");
		user.getAddressList().add("南京");
		//Array数组
		user.setAddressArray(new String[]{"123@tt","424@gg"});
		//Map集合
		user.getAddressMap().put("a","很好");
		user.getAddressMap().put("b", "not good");
		
		
		// 保存
		Session session = ssessionFactory.openSession();// 打开一个新的事务
		Transaction t = session.beginTransaction();// 开始事务
		session.save(user);
		t.commit();// 提交事务
		session.close();// 关闭session，释放资源

	}
	
	@Test
	public void testGet() throws Exception {
		Session session = ssessionFactory.openSession();// 打开一个新的事务
		Transaction t = session.beginTransaction();// 开始事务
		Cuser user = (Cuser) session.get(Cuser.class,8);
		t.commit();// 提交事务
		System.out.println(user.getName());
		System.out.println(user.getAddressSet());
		System.out.println(user.getAddressList().get(2));
		System.out.println(Arrays.toString(user.getAddressArray()));
		System.out.println(user.getAddressMap().get("b"));
		session.close();// 关闭session，释放资源
	}
}
